/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.operation.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.operation.dto.BuySpuDto;
import com.nai365.nart.operation.dto.BuySpuNotifyDto;
import com.nai365.nart.operation.entity.Preemption;
import com.nai365.nart.operation.exception.PreemptionException;
import com.nai365.nart.operation.service.PreemptionService;
import com.nai365.nart.operation.dto.PreemptionDto;
import com.nai365.nart.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 优先购
 *
 * @author baogs
 * @date 2022-07-01 11:13:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/preemption")
@Api(value = "preemption", tags = "优先购管理")
public class PreemptionController {

	private final PreemptionService preemptionService;

	/**
	 * 分页查询
	 *
	 * @param page       分页对象
	 * @param preemption 优先购
	 * @return
	 */
	@ApiOperation(value = "优先购分页查询", notes = "优先购分页查询")
	@GetMapping("/page")
	public R<Page<PreemptionDto>> getPreemptionPage(Page<Preemption> page, Preemption preemption) {
		return R.ok(preemptionService.page(page, Wrappers.query(preemption)));
	}

	@ApiOperation(value = "优先购分页查询", notes = "优先购分页查询")
	@GetMapping("/my")
	public R<Page<PreemptionDto>> myPreemption(Page<Preemption> page) {
		Preemption preemption = new Preemption();
		preemption.setUserId(SecurityUtils.getUser().getId());
		return R.ok(preemptionService.page(page, Wrappers.query(preemption)));
	}

	@ApiOperation(value = "根据Spu查询可用优先购")
	@GetMapping("/list/spu")
	public R<List<PreemptionDto>> listBySpu(@RequestParam Long spuId) {
		try {
			return R.ok(preemptionService.findBySpu(spuId));
		} catch (PreemptionException e) {
			return R.failed(e.getMessage());
		}
	}


	/**
	 * 通过id查询优先购
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R<PreemptionDto> getById(@PathVariable("id") Long id) {
		return R.ok(preemptionService.getPreemptionDtoBy(id));
	}

	/**
	 * 新增优先购
	 *
	 * @param preemption 优先购
	 * @return R
	 */
	@ApiOperation(value = "新增优先购", notes = "新增优先购")
	@SysLog("新增优先购")
	@PostMapping
	public R<Preemption> save(@RequestBody Preemption preemption) {
		preemption.setId(null);
		preemption.setCreateBy(SecurityUtils.getUser().getId());
		preemption.setUpdateBy(SecurityUtils.getUser().getId());
		preemption.setCreateTime(LocalDateTime.now());
		preemption.setUpdateTime(LocalDateTime.now());
		preemption.setUseCount(0);
		if (preemption.getType() == 1) {
			preemption.setCategoryId(null);
			preemption.setArtifactId(null);
		}
		if (preemption.getType() == 2) {
			preemption.setArtifactId(null);
		}
		preemptionService.save(preemption);
		return R.ok(preemption);
	}

	/**
	 * 修改优先购
	 *
	 * @param preemption 优先购
	 * @return R
	 */
	@ApiOperation(value = "修改优先购", notes = "修改优先购")
	@SysLog("修改优先购")
	@PutMapping
	public R<Preemption> updateById(@RequestBody Preemption preemption) {
		preemption.setUpdateBy(SecurityUtils.getUser().getId());
		preemption.setUpdateTime(LocalDateTime.now());
		if (preemption.getType() == 1) {
			preemption.setCategoryId(null);
			preemption.setArtifactId(null);
		}
		if (preemption.getType() == 2) {
			preemption.setArtifactId(null);
		}
		preemptionService.updateById(preemption);
		return R.ok(preemption);
	}

	/**
	 * 通过id删除优先购
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除优先购", notes = "通过id删除优先购")
	@SysLog("通过id删除优先购")
	@DeleteMapping("/{id}")
	public R<Boolean> removeById(@PathVariable Long id) {
		return R.ok(preemptionService.removeById(id));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param preemption 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	public List<Preemption> export(Preemption preemption) {
		return preemptionService.list(Wrappers.query(preemption));
	}

	@PostMapping("/buy/spu")
	public R<String> buySpu(@RequestBody BuySpuDto buySpuDto) {
		try {
			return R.ok(preemptionService.buySpu(buySpuDto), "优先购下单成功");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@PostMapping("/buy/spu/notify")
	public R<Boolean> buySpuNotify(@RequestBody BuySpuNotifyDto buySpuNotifyDto) {
		try {
			return R.ok(preemptionService.buySpuNotify(buySpuNotifyDto), "回调成功！");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}


}
